#!/usr/bin/python

import MySQLdb
import os
import string
import sys
import re
import time
import zipfile
from datetime import datetime
import numpy
import matplotlib
from pylab import *


# check if the number of arguments is correct
if(len(sys.argv) != 5):
    print "Error:\tLess than 6 arguments provided"
    print "Usage:\tdatabase_loader.py host user password database"
    print "Example:\tdatabase_loader.py localhost slamusers slamusers slamusers"
    sys.exit(0)


# params to the script for loading
host = sys.argv[1]
user = sys.argv[2]
password = sys.argv[3]
database = sys.argv[4]

# dump the info just in case
print ""
print "Database Host: %s" % host
print "Database: %s" % database
print ""
print "Waiting 1 seconds before continuing..."
time.sleep(1);



# setup the database connection we're using
conn = MySQLdb.connect (host, user, password, database)
cursor = conn.cursor ()

# the lines we're interested in comparing
lines = ('hulk','shrek', 'otg')
line2 = ('immatureT')

chromosomes=('chr1', 'chr2', 'chr3', 'chr4', 'chr5', 'chr6','chr7', 'chr8', 'chr9', 'chr10', 'chr11' 'chr12', 'chr13', 'chr14', 'chr15', 'chr16','chr17', 'chr18', 'chr19', 'chr20', 'chr21', 'chr22', 'chr23', 'chr24', 'chr25', 'chrM')

for line1 in lines:
    
    sql = "select chromosome, start_position, score from bed_data where line1 = '" + line1 + "' and line2 = 'immatureT' order by chromosome, start_position"

    # perform the query
    try:
        cursor.execute(sql)        
    except Exception, e:
        print e
        print "Error executing SQL Statement: " + sql
    
    # fetch the results and create scores list
    results = cursor.fetchall()
    scores = []
    location = []
    for x in results:
        scores.append(x[2])
        location.append(x[0] + "-" + str(x[1]))
    
    
    #print len(scores)
    
    # plot the scores
    plot(scores)
    xlabel('Location')
    ylabel('Score')
    xlim( -1000, 45000 )
    ylim( -1000, 3000)
    title("Ordered Scores Across All Lines vs 'Input'")
    grid(True)

#xticks(arange(len(location)),location)
savefig('OrderedScores.pdf')
    #show()
    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
